import pyglet
import ctypes
import win32api
import win32gui
import win32con
import random

from shader import shader

ctypes.windll.shcore.SetProcessDpiAwareness(2)


def set_window_through(hwnd, through=True):
    ex_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
    if through:
        new_style = ex_style | win32con.WS_EX_TRANSPARENT
    else:
        new_style = ex_style & ~win32con.WS_EX_TRANSPARENT

    win32gui.SetWindowLong(
        hwnd,
        win32con.GWL_EXSTYLE,
        new_style | win32con.WS_EX_LAYERED
    )

def set_window_borderless(hwnd):
    win32gui.SetWindowLong(
        hwnd, 
        win32con.GWL_STYLE,
        win32con.WS_VISIBLE | win32con.WS_POPUP
    )

def set_window_topmost(hwnd, topmost=True):
    win32gui.SetWindowPos(
        hwnd,
        win32con.HWND_TOPMOST if topmost else win32con.HWND_NOTOPMOST,
        0, 0, 0, 0,
        win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_FRAMECHANGED
    )


class Surface:
    def __init__(self, window, hwnd, x, y):
        self.window = window
        self.hwnd = hwnd
        self.x = x
        self.y = y

    def relocation(self, *args):
        self.window.set_location(self.x, self.y)

monitors = win32api.EnumDisplayMonitors()
screens = pyglet.display.get_display().get_screens()
surfaces = []
for monitor, screen in zip(monitors, screens):
    info = win32api.GetMonitorInfo(monitor[0])
    width = info['Monitor'][2] - info['Monitor'][0]
    height = info['Monitor'][3] - info['Monitor'][1]
    
    window = pyglet.window.Window(
        width=width,
        height=height,
        screen=screen,
        style=pyglet.window.Window.WINDOW_STYLE_TRANSPARENT,
        config=pyglet.gl.Config(
            alpha_size=8,
            double_buffer=True,
            depth_size=0,
            samples=4
        ),
        visible=False
    )
    hwnd = window._hwnd

    wr = win32gui.GetWindowRect(hwnd)
    cr = win32gui.GetClientRect(hwnd)
    th = (wr[3] - wr[1]) - (cr[3] - cr[1])

    o = th // 2
    x = info['Monitor'][0]
    y = info['Monitor'][1] + o - (o - 18)

    window.set_location(x - width, y)

    set_window_through(hwnd)
    set_window_topmost(hwnd)

    surface = Surface(window, hwnd, x, y)
    surfaces.append(surface)


batch = pyglet.graphics.Batch()

ciallo = pyglet.image.load_animation("ciallo.gif")
# ciallo = pyglet.image.load("./resources/img.jpg")
sprite = pyglet.sprite.Sprite(ciallo, batch=batch)
sprite.x = (window.width - sprite.width) // 2
sprite.y = (window.height - sprite.height) // 2

for surface in surfaces:
    window = surface.window
    hwnd = surface.hwnd

    label = pyglet.text.Label(
        "Ciallo～(∠・ω< )⌒★", 
        font_size=24,
        x=window.width,
        y=window.height // 2,
        anchor_x='left',
        anchor_y='center',
        batch=batch
    )
    label.color = (255, 255, 255, 255)
    move_speed = 150

    def update(dt):
        label.x -= move_speed * dt

        label.color = (
            random.randint(0, 255),
            random.randint(0, 255),
            random.randint(0, 255),
            255
        )
        
        if label.x + label.content_width < 0:
            label.x = window.width

    @window.event
    def on_draw():
        window.clear()
        # pyglet.gl.glClearColor(0.5, 0, 0, 0.5)
        with shader:
            batch.draw()

    window.set_visible(True)
    set_window_borderless(hwnd)
    pyglet.clock.schedule_once(surface.relocation, 0.05)
    pyglet.clock.schedule_interval(update, 1/60.0)

pyglet.app.run()